﻿#ifndef CUSTOMLISTWIDGETS_H
#define CUSTOMLISTWIDGETS_H

#include <QTreeWidget>
#include <QHeaderView>
#include <QScrollBar>
#include "common.h"
#include "QTableWidget"
#include "QWidgetAction"
#include "QMenu"
#include "QBoxLayout"
#include "QVariant"
#include <QMutex>
#include <QEvent>

// 悬浮列表框
class EList 
	:public QMenu
{
public:

	EList(QWidget *parent, const struct ListLabel *label, int col_num, QString strCtx = "")
		:QMenu(parent)
	{
		// 重要： 不可捕获焦点 无边框 类ToolTip [12/1/2016 ejitec]
		this->setWindowFlags(Qt::WindowStaysOnTopHint 
			| Qt::WindowDoesNotAcceptFocus
			| Qt::FramelessWindowHint 
			| Qt::Tool
			| Qt::NoDropShadowWindowHint);
		
		/************************************************************************/
		/* 
			// 背景透明和 无边框
			_tab->setAttribute(Qt::WA_TranslucentBackground, true);
			this->setWindowFlags(Qt::FramelessWindowHint);
			//this->setFocusPolicy(Qt::NoFocus);  // 无效

		*/
		/************************************************************************/
		
		_tab = new QTableWidget(this);
		_tab->setWindowFlags(_tab->windowFlags() | Qt::FramelessWindowHint);
		_tab->setLineWidth(0);
		_tab->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		QVBoxLayout *Vlayout = new QVBoxLayout(_tab);
		Vlayout->setSpacing(0);
		_tab->setColumnCount(col_num);

		QStringList labs;
		int nWidth = 0;
		for (int i = 0; i < col_num; i++)
		{
			labs << label[i]._label;
			nWidth += label[i].colwidth;
			_tab->setColumnWidth(i, label[i].colwidth);
		}
		_tab->setHorizontalHeaderLabels(labs);
		_rowHeight = _tab->horizontalHeader()->height() + _tab->lineWidth();
		_tab->setFixedWidth(nWidth + (col_num - 1)*_tab->lineWidth()); //
		_tab->resizeRowsToContents();
		_tab->horizontalHeader()->setStretchLastSection(true);
		_tab->verticalHeader()->setStretchLastSection(true);
		_tab->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
		//		_tab->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // 自动调整会存在问题
		_tab->verticalScrollBar()->setVisible(false);
		_tab->verticalHeader()->setVisible(false);
		_tab->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置表格内容不可编辑  
		_wgtAction = new QWidgetAction(this);
		_wgtAction->setDefaultWidget(_tab);
		this->addAction(_wgtAction);
	}

	virtual void  enterEvent(QEvent *)
	{
		emit aboutToHide();
	}

	virtual ~EList()
	{
		//qDebug() << "~EList() called!!!";
	}

	virtual void  setHeight()
	{
		_tab->setFixedHeight(_rowHeight*(_tab->rowCount() + 1) + _tab->lineWidth());
	}

	void  AppendRow(QStringList row)
	{
		if (_tab->columnCount() != row.size())
		{
			qDebug() << row << "not match column size:" << _tab->columnCount();
			return;
		}
		_tab->setRowCount(_tab->rowCount() + 1);
		int nRow = _tab->rowCount() - 1;

		for (long col = 0; col < _tab->columnCount(); col++)
		{
			QTableWidgetItem* item = new QTableWidgetItem(row[col]);
			item->setTextAlignment(Qt::AlignCenter);
			_tab->setItem(nRow, col, item);
		}
		_tab->setRowHeight(nRow, _rowHeight);
	}

	void focusInEvent(QFocusEvent *event)
	{
		this->hide();
		this->deleteLater();
	}

protected:

private:
	int _rowHeight;
	QTableWidget* _tab;
	QWidgetAction* _wgtAction;

};

class RecordList :public EList
{
public:
	RecordList(QWidget *parent, const struct ListLabel *label, int col_num)
		:EList(parent, label, col_num)
	{
	}
	virtual ~RecordList() {}

	void AppendAllRecord(EagleData_Record_Id* record_id);
	QStringList GenerateCcdRecordRow(EagleData_CcdRecord* ccd_record);


public:
};



// 基础数据 [11/15/2016 ejitec]
class baseCCDItemData
{
public:
	baseCCDItemData() 
	{ 
		data_type = BASE_CLIENT_DATA; 
	}

	CLIENT_DATA_TYPE GetDataType() { return data_type; }

protected:

	CLIENT_DATA_TYPE data_type;
};

Q_DECLARE_METATYPE(baseCCDItemData*)

class DeviceClientData : public baseCCDItemData
{
public:
	DeviceClientData(int device_id, EDeviceType device_type) : baseCCDItemData()
		, _device_id(device_id), _device_type(device_type)
	{
		data_type = DEVICE_CLIENT_DATA;
	}

	int GetDevice(){ return _device_id; }
	EDeviceType GetDeviceType() { return _device_type; }

	DeviceClientData(const DeviceClientData& other)
	{
		_device_id = other._device_id;
		_device_type = other._device_type;
	}

private:

	int _device_id;
	EDeviceType _device_type;
};


class CcdClientData : public DeviceClientData
{
public:
	CcdClientData(int device_id, unsigned short camera_no, EDeviceType device_type)
		: DeviceClientData(device_id, device_type), _camera_no(camera_no){
		data_type = CCD_CLIENT_DATA;
	}

	CcdClientData(unsigned short camera_no, char ok, const DeviceClientData& parent)
		: DeviceClientData(parent), _camera_no(camera_no)
	{
		data_type = CCD_CLIENT_DATA;
	}

	unsigned short GetCameraNo(){ return _camera_no; }

private:
	unsigned short _camera_no;
};

class DevicePopupWindow : public EList
{
public:
	DevicePopupWindow(QWidget *parent, int device_id, const eagle_system_status* status, EDeviceType device_type);
	virtual ~DevicePopupWindow(){};

};

class CLCcdPopupWindow : public EList
{
public:
	CLCcdPopupWindow(QWidget *parent, const eagle_capture_config* ccdconfig, unsigned char diskmap, int device_id, EDeviceType device_type);
	virtual ~CLCcdPopupWindow(){};
};

class SDICcdPopupWindow : public EList
{
public:
	SDICcdPopupWindow(QWidget *parent, const eagle_capture_config* ccdconfig, unsigned char diskmap, int device_id, EDeviceType device_type);
	virtual ~SDICcdPopupWindow(){};
};

// 记录列表 树 [11/15/2016 ejitec]
class baseTreeWidget :public QTreeWidget
{
	Q_OBJECT

public:

	baseTreeWidget(QWidget* parent)
	:QTreeWidget(parent)
	, _curItem(nullptr)
	, _popUpMenu(NULL)
	{
		this->setMouseTracking(true);
		header()->setStretchLastSection(true);
		// 最后一列伸缩到最后 
		//header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
	}

	~baseTreeWidget()
	{

	}

	virtual void focusOutEvent(QFocusEvent *event)
	{
		delete _popUpMenu;
		_popUpMenu = NULL;
	}

public:

	QMutex _mutex;
	QTreeWidgetItem* _curItem;
	EList* _popUpMenu;

private:
	
};


class recListWidgets : public baseTreeWidget
{
	Q_OBJECT

public:

	recListWidgets(QWidget *parent);
	~recListWidgets();

 	virtual void mouseMoveEvent(QMouseEvent *event);
// 
 	virtual void enterEvent(QEvent *event);
// 
 	virtual void leaveEvent(QEvent *event);

	virtual void contextMenuEvent(QContextMenuEvent * event);
	
	void heardInit();

	std::vector<void*> GetSelectedRecords();

protected slots:

	void on_ItemEnter(QTreeWidgetItem* item, int column);

signals:

	void evt_ActRecDel(bool checked = false);
	void evt_ActRecRenam(bool checked = false);
	void evt_ActRecReplay(bool checked = false);
	void evt_ActRecExport(bool checked = false);

private:

};

// dev和 ccd 合并处理 [11/16/2016 ejitec]
class devListWidgets : public baseTreeWidget
{
	Q_OBJECT

public:

	devListWidgets(QWidget *parent);
	~devListWidgets();

	virtual void mouseMoveEvent(QMouseEvent *event);

	virtual void leaveEvent(QEvent *event);

	void heardInit();

	void setCheckConnection();

	void disConnectCheck();

protected:


	void showPoptimeOut(QTreeWidgetItem* item);
	void ChangeDeviceItemText(QTreeWidgetItem* ItemToChange, eagle_system_status* system_status, EDeviceType device_type);
	void ChangeCcdItemText(QTreeWidgetItem* ItemToChange, eagle_capture_config* capture_status, unsigned char disk_mapping, int device_id);
	void RefreshItems();

protected slots:

	void on_ItemChanged(QTreeWidgetItem* item, int column);
	void on_ItemEnter(QTreeWidgetItem* item, int column);
	void on_freshItems();

private:

	QTimer* _timer;
	QVector<QIcon> _icons;
};

#endif // CUSTOMLISTWIDGETS_H
